#!/bin/bash
set -euo pipefail

#==============================================================#
# File      :   pg-report
# Ctime     :   2021-09-17
# Mtime     :   2021-09-17
# Desc      :   Generate pgbadger HTML report to /pg/stat/logs
# Path      :   /pg/bin/pg-badger
# Depend    :   pgbadger, /pg/stat
# Author    :   Vonng(fengruohang@outlook.com)
# Copyright (C) 2018-2022 Ruohang Feng
#==============================================================#

# usage
# pg-report       incremental report for today's log
# pg-report full  full report for all historic log

# run as postgres
if [[ "$(whoami)" != "postgres" ]]; then
	echo "run this as dbsu postgres"
	exit 1
fi
mkdir -p /pg/stat/logs

MODE=${1-''}

if [[ ${MODE} == "full" ]]; then
	pgbadger \
	   -I /pg/data/log/*.csv \
	   -f csv \
	   --outdir /pg/stat/logs \
	   --wide-char \
	   --average 1 \
	   --sample 3
else
    LATEST_LOGS="$(find /pg/data/log -name '*.csv' -mtime 0)"
	pgbadger \
	   -I ${LATEST_LOGS} \
	   -f csv \
	   --outdir /pg/stat/logs \
	   --wide-char \
	   --average 1 \
	   --sample 3
fi


